package utils

import (
	"gallery/crawler"
	"log"
	"net/http"
	"net/url"
	"os"
	"strings"

	"github.com/fatih/color"
)

// MustRequest 生成指定响应头的Request
func MustRequest(method, uri string, body url.Values) *http.Request {
	req, err := http.NewRequest(method, uri, strings.NewReader(body.Encode()))
	if err != nil {
		log.Fatalln("请求构建失败！")
	}

	req.Header.Set("cookie", crawler.Cookie)
	req.Header.Set("user-agent", crawler.Ua)
	req.Header.Set("keep-alive", "true")

	return req
}

// MustParseURL 解析url
func MustParseURL(uri string) (*url.URL, url.Values) {
	u, err := url.Parse(uri)
	if err != nil {
		color.Red("URL解析失败")
		os.Exit(2)
	}
	return u, u.Query()
}
